/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package it.polimi.se2.meteocal.control;

import it.polimi.se2.meteocal.entity.Event;
import it.polimi.se2.meteocal.entity.EventNotification;
import it.polimi.se2.meteocal.entity.Participation;
import it.polimi.se2.meteocal.entity.ParticipationNotification;
import it.polimi.se2.meteocal.entity.Users;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;

/**
 *
 * @author Ugo
 */
@Stateless
public class NotificationManager  {
    private static final long serialVersionUID = 1L;
  
    @PersistenceContext
    public EntityManager empn;
    
    @PersistenceContext
    public EntityManager emen;
    
    public void create(ParticipationNotification entity) {
        empn.persist(entity);
    }
     
    public void create(EventNotification entity){
        emen.persist(entity);
    }
    
    public void edit(ParticipationNotification entity) {
        empn.merge(entity);
    } 
    
    public void edit(EventNotification entity) {
        emen.merge(entity);
    }

    public void remove(ParticipationNotification entity) {
        empn.remove(empn.merge(entity));
    }
   
    public void remove(EventNotification entity) {
        emen.remove(emen.merge(entity));
    }
    
    public List<ParticipationNotification> findParticipationNotificationByParticipation(Participation participation){
        try{
        return empn.createNamedQuery(ParticipationNotification.findByParticipation, ParticipationNotification.class)
                .setParameter("participation", participation)
                .getResultList();
        }
         catch(NoResultException e){
            return null;
        }
    }
    
    public ParticipationNotification findParticipationNotificationByParticipationAndType(Participation participation,Long type){
      try{
        return empn.createNamedQuery(ParticipationNotification.findByParticipationAndType, ParticipationNotification.class)
                .setParameter("participation", participation)
                .setParameter("type", type)
                .getSingleResult();
        }
        catch(NoResultException e){
            return null;
        }
    }
     
    public ParticipationNotification findParticipationNotificationByUserNotSeen(Users user){
        try{
               return empn.createNamedQuery(ParticipationNotification.findByUserNotSeen, ParticipationNotification.class)
                .setParameter("user", user)
                .setMaxResults(1)
                .getSingleResult();
        }
        catch(NoResultException e){
            return null;
        }    
    }
     
    public List<ParticipationNotification> findParticipationNotificationByUser1(Users user){
        try{
               return empn.createNamedQuery(ParticipationNotification.findByUser, ParticipationNotification.class)
                .setParameter("user", user)
                .setParameter("type", Long.valueOf(1))
                .getResultList();
        }
        catch(NoResultException e){
            return null;
        }
    }
    
    public List<ParticipationNotification> findParticipationNotificationByUser2(Users user){
        try{
               return empn.createNamedQuery(ParticipationNotification.findByUser, ParticipationNotification.class)
                .setParameter("user", user)
                .setParameter("type", Long.valueOf(2))
                .getResultList();
        }
        catch(NoResultException e){
            return null;
        }  
    }
     
    public List<ParticipationNotification> findParticipationNotificationByUser3(Users user){
        try{
               return empn.createNamedQuery(ParticipationNotification.findByUser, ParticipationNotification.class)
                .setParameter("user", user)
                .setParameter("type", Long.valueOf(3))
                .getResultList();
        }
        catch(NoResultException e){
            return null;
        } 
    }
    
    public EventNotification findEventNotificationByEventNotSeen(Event event){
        try{
               return emen.createNamedQuery(EventNotification.findByEventNotSeen, EventNotification.class)
                .setParameter("event", event)
                .getSingleResult();
        }
        catch(NoResultException e){
            return null;
        }  
    }
    
    public EventNotification findEventNotificationByEvent(Event event){
        try{
               return emen.createNamedQuery(EventNotification.findByEvent, EventNotification.class)
                .setParameter("event", event)
                .getSingleResult();
        }
        catch(NoResultException e){
            return null;
        }
    }
    
}